import axios from "axios";
import { queryWorkingDay } from "./queryDay.js";
import { authorization, workContentList } from "./config.js";

// 提交每月的工时
export const submitMonthWorkHour = async () => {
  const workDays = await queryWorkingDay();

  for (let index = 0, len = workContentList.length; index < len; index++) {
    await submitEachDayData(workDays[index], workContentList[index]);
  }
};

/**
 * 提交每天的工时数据
 * @param {*} workDate    工作日期
 * @param {*} workContent 工作内容
 */
const submitEachDayData = (workDate, workContent) => {
  return new Promise((resolve, reject) => {
    const url = "https://xxx/api/xxx";
    let headers = {
      authorization,
      "Content-Type": "application/json",
    };

    const params = {
      workDate,
      workContent,
      tapdId: null,
      groupId: 12,
      projectId: 159,
      lineId: 2,
      taskId: 16,
      workHours: 8,
    };

    setTimeout(() => {
      axios
        .post(url, params, { headers })
        .then(({ data }) => {
          const { ret, retdata, retmsg } = data;
          //   if (ret === 0) {
          console.log(`${workDate}--${retmsg}`);
          resolve("ok");
          //   }
        })
        .catch((err) => reject(err));
    }, 500);
  });
};
